﻿using System.Web.UI;
using System.Web.UI.WebControls;
using HelpersDotNet.Configuration.Provider;
using System;
using System.Collections.Specialized;
using System.Diagnostics;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Linq;
using HelpersDotNet.Extensions;
using System.Collections.Generic;

namespace HelpersDotNet.Web.UI.TextEditorProvider
{
    public abstract class TextEditorProvider : HelpersDotNet.Configuration.Provider.ProviderBase
    {
        public abstract Control TextEditor { get; }
        public abstract Unit Width { get; set; }
        public abstract Unit Height { get; set; }
        public abstract string Text { get; set; }

        protected abstract string[] modes { get; }

        private string[] _availableModes = null;
        public string[] AvailableModes
        {
            get
            {
                if (_availableModes == null)
                    _availableModes = modes.Distinct().ToArray();
                return _availableModes;
            }
        }
        private string _mode = "default";
        public virtual string Mode
        {
            get { return _mode; }
            set
            {
                try
                {
                    _mode = AvailableModes.Single(m => m == value);
                }
                catch
                {

                    string s = "";
                    foreach (string m in AvailableModes)
                        s += m + " ";
                    throw new ArgumentOutOfRangeException("Invalid mode.\nAvailable modes are " + s);
                }

                ChangeMode(_mode);
            }
        }

        public abstract void ChangeMode(string newMode);

        #region ConfigSection

        /// <summary>
        /// Default Height is 400px
        /// </summary>
        public Unit DefaultHeight
        {
            get
            {
                string height = this.Configuration["defaultHeight"] ?? "400px";
                return Unit.Parse(height);
            }
        }

        /// <summary>
        /// Default width is 400px
        /// </summary>
        public Unit DefaultWidth
        {
            get
            {
                string width = this.Configuration["defaultWidth"] ?? "400px";
                return Unit.Parse(width);
            }
        }

        public string DefaultMode
        {
            get { return this.Configuration["defaultMode"]; }
        }

        #endregion

    }

    public abstract class TextEditorService
    {
        #region Provider Model

        private static ProviderRepository<TextEditorProvider> _providers =
            new ProviderRepository<TextEditorProvider>("helpersdotnet/texteditor");

        public static TextEditorProvider Provider
        {
            get { return _providers.Provider; }
        }

        public static ProviderCollection<TextEditorProvider> Providers
        {
            get { return _providers.Providers; }
        }

        #endregion
    }

    public class TextBoxTextEditorProvider : TextEditorProvider
    {
        private TextBox _editor;

        public TextBoxTextEditorProvider()
        {
            _editor = new TextBox();
            _editor.Height = base.DefaultHeight;
            _editor.Width = base.DefaultWidth;
        }

        public override Control TextEditor
        {
            get { return _editor; }
        }

        public override Unit Width
        {
            get { return _editor.Width; }
            set { _editor.Width = value; }
        }

        public override Unit Height
        {
            get { return _editor.Height; }
            set { _editor.Height = value; }
        }

        public override string Text
        {
            get { return _editor.Text; }
            set { _editor.Text = value; }
        }

        public override void ChangeMode(string newMode)
        {
            // don't do anything coz no mode.
        }
        
        protected override string[] modes
        {
            get { return new string[] { "default" }; }
        }
    }

}
